from rich.text import Text
from rich.console import Console
console = Console()
text = Text("HellosWorld!")
# text.stylize(0, 6, 'bold yellow')# 0为style的初始位置，6为style的结束位置
text.stylize(1, 7, "bold black")
console.print(text)
text = Text()
text.append("Hello", style="bold green")
text.append(" World!")
console.print(text)

# assemble，将小块string合并
text = Text.assemble(("Hello", "bold magenta"), " World!")
console.print(text)

# Highlights
from rich.console import Console
from rich.highlighter import RegexHighlighter
from rich.theme import Theme

class EmailHighlighter(RegexHighlighter):
    """Apply style to anything that looks like an email."""

    base_style = "example."# base style name, 任何style中以base_style起始的style都会遵循highlights寻找方式

    highlights = [r"(?P<email>[\w-]+@([\w-]+\.)+[\w-]+)"]# (?<email>)创建一个正则匹配的名称, [/w-]匹配unicode编码所有字符和-
                                                         # +重复一次或者无数次
                                                         # ([\w-]+\.)匹配任意字符和一个.


theme = Theme({"example.email": "bold magenta"})
console = Console(highlighter=EmailHighlighter(), theme=theme)
console.print("Send funds to money@example.example.ieee.org")

console = Console(theme=theme)
highlight_emails = EmailHighlighter()
console.print(highlight_emails("Send funds to money@example.org"))
# print(highlight_emails("Send funds to money@example.org")) # print无法输出颜色格式
from random import randint

from rich import print
from rich.highlighter import Highlighter


class RainbowHighlighter(Highlighter):
    def highlight(self, text):
        for index in range(len(text)):
            text.stylize(index, index + 1, str(randint(16, 255)))


rainbow = RainbowHighlighter()
print(rainbow("I must not fear. Fear is the mind-killer."))

# syntax
from rich.console import Console
console = Console()
console.print('[bold red on black]Hello World![/bold red on black]')
console.print('[bold red on black]bold red [red] red [/] not bold and red' )# 不停止某一个tag那么它会一直执行到最后

console.print('visit [link=https://www.baidu.com]baidu[/link]')# mac terminal不支持超链接
# Escape
console.print('print [[bold]]')# print [bold]
import rich
console.print(rich.markup.escape('print [bold]'))# same as above
